package com.kcyu.sfherp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kcyu.sfherp.entity.LoginUser;
import com.kcyu.sfherp.entity.UserRecords;
import com.kcyu.sfherp.entity.utilEntity.PageEntity;
import com.kcyu.sfherp.mapper.ms.UserRecordsMapper;
import com.kcyu.sfherp.service.UserRecordsService;
import com.kcyu.sfherp.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class UserRecordsServiceImpl implements UserRecordsService {

    @Autowired
    private UserRecordsMapper userRecordsMapper;

    @Override
    public ResponseResult addRecord(UserRecords userRecords) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        userRecords.setOperateTime(timestamp);
        userRecords.setUsername(user.getUsername());
        userRecordsMapper.insert(userRecords);
        return new ResponseResult(200, "添加成功");
    }

    @Override
    public PageEntity<UserRecords> pageRecord(PageEntity<UserRecords> pageEntity) {
        PageHelper.startPage(pageEntity.getPage(), pageEntity.getPageSize());
        QueryWrapper<UserRecords> userRecordsQueryWrapper = new QueryWrapper<>();
        userRecordsQueryWrapper.like("username", pageEntity.getQueryCondition().getUsername());
        userRecordsQueryWrapper.like("value", pageEntity.getQueryCondition().getValue());
        userRecordsQueryWrapper.like("url", pageEntity.getQueryCondition().getUrl());
        userRecordsQueryWrapper.orderByDesc("operate_time");
        if(Objects.nonNull(pageEntity.getQueryCondition().getStartTime())) {
            userRecordsQueryWrapper.ge("operate_time", pageEntity.getQueryCondition().getStartTime());
        }

        if(Objects.nonNull(pageEntity.getQueryCondition().getEndTime())) {
            userRecordsQueryWrapper.le("operate_time", pageEntity.getQueryCondition().getEndTime());
        }


        List<UserRecords> userRecords = userRecordsMapper.selectList(userRecordsQueryWrapper);
        PageInfo<UserRecords> pageInfo = new PageInfo<>(userRecords);
        pageEntity.setRecords(pageInfo.getList());
        pageEntity.setTotal(pageInfo.getTotal());
        return pageEntity;
    }

    @Override
    public String addRecordImpl(UserRecords userRecords) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        userRecords.setOperateTime(timestamp);
        userRecordsMapper.insert(userRecords);
        return "添加成功";
    }

    @Override
    public ResponseResult getAllRecords() {
        List<UserRecords> userRecords = userRecordsMapper.selectList(new QueryWrapper<>());
        return new ResponseResult(200, "获取成功", userRecords);

    }

    @Override
    public ResponseResult queryRecords(UserRecords userRecords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<UserRecords> wrapper = new QueryWrapper<>();
        wrapper.like("username", userRecords.getUsername());
        List<UserRecords> records = userRecordsMapper.selectList(wrapper);
        PageInfo<UserRecords> pageInfo = new PageInfo<>(records);
        return new ResponseResult(200, "获取成功", pageInfo);
    }

    @Override
    public ResponseResult queryActiveUser() {
        QueryWrapper<UserRecords> wrapper = new QueryWrapper<>();
        wrapper.groupBy("username");
        List<UserRecords> records = userRecordsMapper.selectList(wrapper);
        return new ResponseResult(200, "获取成功", records);
    }

    @Override
    public ResponseResult deleteRecords(Integer recordsId) {
        int i = userRecordsMapper.deleteById(recordsId);
        if(i != 0) {
            return new ResponseResult(200, "删除成功");
        } else {
            return new ResponseResult(300, "删除失败");
        }

    }
}
